<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>点击空白处执行函数</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      background-color: red;
    }
  </style>
</head>

<body>
  原生js实现点击目标区域外侧触发事件
  https://www.cnblogs.com/axu1997/p/13040604.html

  js点击空白无效区域关闭弹窗
  https://blog.csdn.net/weixin_39417767/article/details/114694420?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-114694420-blog-115264278.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-114694420-blog-115264278.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=1
  <div class="box">
    <div class="box-item">
      简简单单的子class内容<br />
      简简单单的子class内容<br />
      简简单单的子class内容<br />
      简简单单的子class内容<br />
    </div>
  </div>
</body>
<script>
  //封装
  //封装
  function clickoutSide(nameClass, callback) {
    // 全局注册点击事件
    document.onclick = function (e) {
      // 若点击元素为目标元素或目标元素的子元素，则返回
      const targetElement = e.target.closest("." + nameClass);
      if (
        (targetElement && targetElement.classList.contains(nameClass))
      ) {
        return;
      }
      // 否则执行回调函数
      callback();
    };
  }
  clickoutSide("box", function () {
    console.log("点击了外部");
  });
</script>

</html>